﻿using Scap.Info;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Scap.Core
{
    public class DeviceTaskHandlerUpload : DeviceTaskHandler
    {
        private int iMachineNumber = 1;



        public DeviceTaskHandlerUpload(DeviceTaskModel model)
            : base(model)
        {

        }



        public override void Run()
        {
            ScapDevice dev = _model.Device;
            
            _logger.Info("{0} | Uploading...", dev.IpAddress);

            if (dev.Status == DeviceStatus.Offline)
            {
                _logger.Info("{0} | Uploading...[FAILED]: device offline", dev.IpAddress);
                return;
            }

            // PENDING logic to get employees to upload to the device           
           
            CommandHandler cHandler = new CommandHandler();
            ScapCommandCollection coll = cHandler.GetUploadCommands();

            foreach (ScapCommand cmd in coll)
            {
                try
                {
                    _logger.Info("{0} | {1} | Uploading user info...", dev.IpAddress, cmd.EmployeeId);
                    UploadUser(cmd);
                    cHandler.UpdateUploadCommand(cmd, ScapCommand.DONE);
                    _logger.Info("{0} | {1} | Uploading user info...[OK]", dev.IpAddress, cmd.EmployeeId);
                }
                catch (Exception ex)
                {
                    _logger.Info("{0} | {1} | Uploading user info...[FAILED]: {2}", dev.IpAddress, cmd.EmployeeId, ex.Message);
                }
            }
        }

        private void UploadUser(ScapCommand cmd)
        {            
            _zkDevice.EnableDevice(iMachineNumber, false);

            EmployeeHandler eHandler = new EmployeeHandler();
            ScapEmployee employee = eHandler.GetScapEmployee(cmd.EmployeeId);
            ScapEmployeeEnrollCollection enrollments = eHandler.GetEmployeeEnrollments(employee.Id);
            

            for (int i = 0; i < enrollments.Count; i++)
            {
                ScapEmployeeEnroll enroll = enrollments[i];

                int idwErrorCode = 0;
                string sdwEnrollNumber = employee.Id.ToString();
                string sName = employee.FullName;
                int idwFingerIndex = enroll.Index;
                string sTmpData = enroll.DataStr;
                int iPrivilege = 0; // non admin
                string sPassword = string.Empty; // none
                int iFlag = 0; // ????
                bool bEnabled = true;

                //upload user information to the device
                if (_zkDevice.SSR_SetUserInfo(iMachineNumber, sdwEnrollNumber, sName, sPassword, iPrivilege, bEnabled))
                {
                    //upload templates information to the device
                    //_zkDevice.SetUserTmpExStr(iMachineNumber, sdwEnrollNumber, idwFingerIndex, iFlag, sTmpData);
                    _zkDevice.SSR_SetUserTmpStr(iMachineNumber, sdwEnrollNumber, idwFingerIndex, enroll.DataStr);
                    _logger.Info("{0} | {1} | {2} | Upload fingerprint template...[OK]", _device.IpAddress, employee.Id, enroll.Id);
                }
                else
                {
                    _zkDevice.GetLastError(ref idwErrorCode);
                    _logger.Error("{0} | {1} | {2} | Upload fingerprint template...[FAILED]:ErrorCode=" + idwErrorCode.ToString(), _device.IpAddress, employee.Id, enroll.Id);
                    _zkDevice.EnableDevice(iMachineNumber, true);
                    return;
                }
            }

            //the data in the device should be refreshed
            _zkDevice.RefreshData(iMachineNumber);
            _zkDevice.EnableDevice(iMachineNumber, true);            
        }
    }
}
